#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pygtk
pygtk.require("2.0")
import gtk.glade
import shutil

from goldgenerator import *

class Popup:
    def saveImage(self, widget):
        chooser = gtk.FileChooserDialog(title=None,action=gtk.FILE_CHOOSER_ACTION_SAVE,
                                        buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_SAVE,gtk.RESPONSE_OK))
        chooser.set_current_name("wykres.png")       
        response = chooser.run()
        
        if response == gtk.RESPONSE_OK:
            filename = chooser.get_filename()
            shutil.copy (self.file, filename)

        chooser.destroy()
        
    def draw(self, img, text):
        self.file = img
        self.img.set_from_file(img)
        self.text.set_text(text)
        self.window.show_all()
        
    def __init__(self):
        self.gladefile = "popup.glade"
        self.wTree = gtk.glade.XML(self.gladefile)
        self.window = self.wTree.get_widget("main_window")
        
        self.img = self.wTree.get_widget("image1")
        self.text = self.wTree.get_widget("label1")
            
        dict = { 
            "on_button1_clicked" : self.saveImage
        }
        
        self.wTree.signal_autoconnect(dict)
        
class Gui:        
    def about(self, widget):
        D = gtk.AboutDialog()
        D.set_name("Projekt SSRW - Generator kodów Golda")
        D.set_version("1.0")
        D.set_authors(("Piotr Ładyński", "Michał Siemiończyk"))
        D.set_comments("Systemy Szerokopasmowe z Rozpraszaniem Widma\nWarszawa 2010")
        D.show()
        D.run()
        D.destroy()
    
    def drawCorrel(self, widget):        
        regCount = int(self.wTree.get_widget("regCb1").get_active_text())
        regSize = int(self.wTree.get_widget("regCb2").get_active_text())
        
        goldGen = GeneratorGolda()
        goldGen.setRegLength(regSize)
        goldGen.setGensCount(2)

	if widget.get_name() == "button4":
            a = 1
	    b = 2
        elif widget.get_name() == "button5":
	    a = 2
	    b = 3
        elif widget.get_name() == "button6":
            a = 1
	    b = 3
        	
        odczepy = {}
        odczepy[0] = []
        odczepy[1] = []
        
        for i in range (1, regSize+1):
            odczepy[0].append(self.wTree.get_widget("reg%s_%s" % (a, i)).get_active())
            odczepy[1].append(self.wTree.get_widget("reg%s_%s" % (b, i)).get_active())
        
        for i in range(0, 2):
            odczep = Odczep()
            odczep.setOdczep(odczepy[i])
            goldGen.setOdczepy(i, odczep)
        
        goldGen.generate()        
        
        out = goldGen.printOutput2(a, b)
        
        f = createGraph(correlation(goldGen.getmSeq(0), goldGen.getmSeq(1)), u'Korelacja skrośna %s-%s' % (a, b))
        my_pop = Popup()
        my_pop.draw(f, out)
    
    def drawReg(self, widget):
        number = widget.get_name()
        j = int(number[-1])
        regSize = int(self.wTree.get_widget("regCb2").get_active_text())        
        goldGen = GeneratorGolda()
        goldGen.setRegLength(regSize)
        goldGen.setGensCount(1)
        odczep = Odczep()
        odczepy = []
        
        for i in range (1, regSize+1):
            odczepy.append(self.wTree.get_widget("reg%s_%s" % (j, i)).get_active())
        odczep.setOdczep(odczepy)

        goldGen.setOdczepy(1, odczep)
        goldGen.generate()        
        
        out = goldGen.printOutput(False)
        
        f = createGraph(correlation(goldGen.getmSeq(0), goldGen.getmSeq(0)), u'Autokorelacja R%s' % j)
        
        my_pop = Popup()
        my_pop.draw(f, out)
        
    
    def regCountChanged(self, widget):
        regCount = int(self.wTree.get_widget("regCb1").get_active_text())
        
        if regCount == 2:
            self.wTree.get_widget("register_3").hide()            
            self.wTree.get_widget("button3").hide()
            self.wTree.get_widget("button5").set_sensitive(False)
            self.wTree.get_widget("button6").set_sensitive(False)
        else:
            self.wTree.get_widget("button5").set_sensitive(True)
            self.wTree.get_widget("button6").set_sensitive(True)
            self.wTree.get_widget("register_3").show()
            self.wTree.get_widget("button3").show()
    
    def regSizeChanged(self, widget):
        regSize = int(self.wTree.get_widget("regCb2").get_active_text())
        
        for i in range (1, 4):
            for j in range (1, regSize+1):
                self.wTree.get_widget("reg%s_%s" % (i, j)).set_sensitive(True)
                self.wTree.get_widget("reg%s_%s" % (i, j)).set_active(False)
            for j in range (regSize, 6):
                self.wTree.get_widget("reg%s_%s" % (i, j)).set_sensitive(False)
                self.wTree.get_widget("reg%s_%s" % (i, j)).set_active(False)
            self.wTree.get_widget("reg%s_%s" % (i, regSize)).set_active(True)
        
    def buttonClicked(self, widget):
            regCount = int(self.wTree.get_widget("regCb1").get_active_text())
            regSize = int(self.wTree.get_widget("regCb2").get_active_text())
            
            goldGen = GeneratorGolda()
            goldGen.setGensCount(regCount)
            goldGen.setRegLength(regSize)
            
            odczepy = {}
            
            for i in range (1, regCount+1):
                odczepy[i] = []
                for j in range (1, regSize+1):
                    odczepy[i].append(self.wTree.get_widget("reg%s_%s" % (i, j)).get_active())
            
            for i in range(0, regCount):
                odczep = Odczep()
                odczep.setOdczep(odczepy[i+1])
                goldGen.setOdczepy(i, odczep)
            
            goldGen.generate()
            
            out = goldGen.printOutput(True)
            
            f = createGraph(correlation(goldGen.getGold(),goldGen.getGold()), u'Autokorelacja kodu Golda')
    
            my_pop = Popup()
            my_pop.draw(f, out)
                
    def __init__(self):
        self.gladefile = "projekt.glade"
        self.wTree = gtk.glade.XML(self.gladefile)
        
        self.window = self.wTree.get_widget("main_window")
        if (self.window):
            self.window.connect("destroy", gtk.main_quit)
        
        self.wTree.get_widget("regCb1").set_active(0)
        self.wTree.get_widget("regCb2").set_active(1)
        
        dict = { 
            "on_genGoldBtn_clicked" : self.buttonClicked,
            "on_regCb1_changed" : self.regCountChanged,
            "on_regCb2_changed" : self.regSizeChanged,
            "on_button1_clicked" : self.drawReg,
            "on_button2_clicked" : self.drawReg,
            "on_button3_clicked" : self.drawReg,
            "on_button4_clicked" : self.drawCorrel,
            "on_button5_clicked" : self.drawCorrel,
            "on_button6_clicked" : self.drawCorrel,
            "on_aboutBtn_clicked" : self.about,
            "on_corrBtn_clicked" : self.drawCorrel
        }
        
        self.wTree.signal_autoconnect(dict)
        
        
        self.window.show_all()
        
        self.regCountChanged(None)
        self.regSizeChanged(None)

if __name__ == "__main__":
    my_gui = Gui()
    gtk.main()
